package com.su.gulimall.member.service.impl;

import com.su.gulimall.member.dao.MemberLevelDao;
import com.su.gulimall.member.entity.MemberLevelEntity;
import com.su.gulimall.member.exception.PhoneExistException;
import com.su.gulimall.member.vo.LoginVo;
import com.su.gulimall.member.vo.MemberRegisterVo;
import com.su.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.su.common.utils.PageUtils;
import com.su.common.utils.Query;

import com.su.gulimall.member.dao.MemberDao;
import com.su.gulimall.member.entity.MemberEntity;
import com.su.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelDao memberLevelDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveRegisterMember(MemberRegisterVo vo) {
        MemberEntity entity = new MemberEntity();
        MemberDao memberDao=this.baseMapper;
        //对关键信息进行补全
        MemberLevelEntity memberLevelEntity =memberLevelDao.getDefaultLevel();
        entity.setLevelId(memberLevelEntity.getId());
        //保存部分数据
        entity.setUsername(vo.getUserName());
        //密码信息需要加密存储，防止被破坏,MD5加盐机制进行操作，加密encode,解密match,返回true即为密码正确
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(vo.getPassword());
        entity.setPassword(encode);
        //检查手机号是否唯一，通过异常机制来实现
        checkPhoneUnique(vo.getPhone());
        entity.setMobile(vo.getPhone());
        //保存会员信息
        memberDao.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException{
        Integer integer = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(integer>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public MemberEntity login(LoginVo vo) {
        String acct = vo.getLoginAcct();
        String password = vo.getPassword();
        //从数据库查询出加密的密码和前端传入的密码进行对比，从而验证用户的合法性
        MemberDao memberDao=this.baseMapper;
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username", acct).or().eq("mobile", acct));
        //查无此人
        if(memberEntity==null)
        {
            return null;
        }
        //查到有这个人，进行密码对比
        else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean b = encoder.matches(password, memberEntity.getPassword());
            if(b)
            {
                //密码对比成功
                return memberEntity;
            }
            //密码对比失败
            return null;
        }
    }

    @Override
    public MemberEntity authLogin(SocialUserVo vo) {
        //具有登录和注册的合并逻辑
        Long uid = vo.getUid();
        //判断当前社交用户是否已经登录过系统
        MemberDao memberDao=this.baseMapper;
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("uid", uid));
        if(memberEntity!=null)
        {
            //该用户已经注册,对其信息进行更新
            MemberEntity update = new MemberEntity();
            update.setId(vo.getUid());
            update.setAccessToken(vo.getAccessToken());
            update.setExpiresIn(vo.getExpiresIn());
            memberDao.updateById(update);
            //获取值并返回
            memberEntity.setAccessToken(vo.getAccessToken());
            memberEntity.setExpiresIn(vo.getExpiresIn());
            return memberEntity;
        }else {
           //对该社交用户进行注册
            MemberEntity register = new MemberEntity();
            register.setUid(vo.getUid());
            register.setAccessToken(vo.getAccessToken());
            register.setExpiresIn(vo.getExpiresIn());
            register.setNickname(vo.getName());
            //获取到gitee的账户信息
            memberDao.insert(register);
            return register;
        }
    }
}